package io.hxci.modules.tea.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.hxci.common.annotation.SysLog;
import io.hxci.common.utils.PageUtils;
import io.hxci.common.utils.R;
import io.hxci.modules.sys.controller.AbstractController;
import io.hxci.modules.tea.domain.TeaDict;
import io.hxci.modules.tea.domain.TeaScientific;
import io.hxci.modules.tea.domain.TeaTreatise;
import io.hxci.modules.tea.service.TeaDictService;
import io.hxci.modules.tea.service.TeaScientificService;
import io.hxci.modules.tea.service.TeaTreatiseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("tea/scientific")
public class TeaScientificController extends AbstractController {
    @Autowired
    private TeaScientificService teaScientificService;
    @Autowired
    private TeaDictService teaDictService;

    @SysLog("查看科研成果转化")
    @RequestMapping("/query")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = teaScientificService.queryPage(params);
        return R.ok().put("page", page);
    }

    @SysLog("更新科研成果转化")
    @RequestMapping("saveOrUpdate")
    public R save(@RequestBody TeaScientific teaScientific){
        if(teaScientific.getUserId() == null){
            teaScientific.setUserId(getUserId());
        }


        teaScientificService.saveOrUpdate(teaScientific);
        return R.ok();
    }

    @SysLog("删除科研成果转化")
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] id){
        teaScientificService.removeByIds(Arrays.asList(id));
        return R.ok();
    }

    @SysLog("编辑专著和主编教材")
    @RequestMapping("/edit/{id}/{type}")
    @ResponseBody
    public R edit(@PathVariable("id") Integer id,@PathVariable("type") String type){
        TeaScientific edit = teaScientificService.getById(id);
        List<TeaDict> list = teaDictService.list(new QueryWrapper<TeaDict>().like(StringUtils.isNotBlank(type),"type", type));
        return R.ok().put("edit", edit).put("kdoptions",list);
    }

}
